package com.home.truc.GUI;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;

import javassist.expr.NewArray;

import javax.swing.JPanel;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import java.awt.FlowLayout;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import com.home.Common.Common;
import com.home.Entity.Ban;
import com.home.Entity.KhuVucBan;
import com.home.Entity.Mon;
import com.home.Entity.NhomMon;
import com.home.Entity.TTGoiMon;
import com.home.duy.GUI.ParentFrame;
import com.home.duy.code.DAO.BanDAO;
import com.home.duy.code.DAO.KhuVucBanDAO;
import com.home.duy.code.DAO.MonDAO;
import com.home.duy.code.DAO.NhomMonDAO;
import com.home.duy.code.DAO.TTGoiMonDAO;
import com.home.truc.code.components.MyItemBan;
import com.home.truc.code.components.MyItemThongTinGoiMon;
import com.home.truc.code.components.MyJPanelKhuVucBan;
import com.home.truc.code.components.MyJlistItemMon;
import com.home.truc.code.components.MyJlistItemNhomMon;
import com.home.truc.code.components.ThongTinGoiMonComponent;
import com.home.truc.code.components.ThongTinMonComponent;
import com.home.truc.code.process.MyControl;

import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListSelectionModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;

import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.SystemColor;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import javax.swing.border.TitledBorder;
import java.awt.Color;
import javax.swing.JPopupMenu;
import java.awt.Component;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.JFrame;

public class frmBanHang extends JInternalFrame {
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private ParentFrame parent;
	ThongTinGoiMonComponent ttGoiMon = null;
	ThongTinMonComponent ttMon = null;
	/**
	 * This is the xxx default constructor
	 */
	public frmBanHang(ParentFrame pa) {
		super();
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameClosing(InternalFrameEvent arg0) {
				TamDung();
			}
		});
		setClosable(true);
		setBorder(null);
		/*
		setRootPaneCheckingEnabled(false);  
		javax.swing.plaf.InternalFrameUI ui  = this.getUI();  
		((javax.swing.plaf.basic.BasicInternalFrameUI)ui).setNorthPane(null);  
		 */
		this.parent = pa;
		initialize();

		this.ttGoiMon = new ThongTinGoiMonComponent(parent);
		this.ttMon = new ThongTinMonComponent(parent,this.listMon, this.panelGoiMon,this.tabbedPaneBan);
		LoadData();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(777, 459);
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	JPanel panelStatus = null;
	JList listNhomMon = null;
	JLabel lblDanhSachNhom = null;
	JList listMon = null;
	JLabel lblDanhSachMon = null;
	JTabbedPane tabbedPaneBan = null;
	JPanel panelGoiMon = null;
	JSplitPane splitPane_1 = null;
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout(0, 0));
			
			panelStatus = new JPanel();
			jContentPane.add(panelStatus, BorderLayout.SOUTH);
			panelStatus.setSize((int) jContentPane.getSize().getWidth(), 50);
			panelStatus.setLayout(new BorderLayout(0, 0));
			
			final JPanel panelLMonDuocGoi = new JPanel();
			panelLMonDuocGoi.setBorder(null);
			panelStatus.add(panelLMonDuocGoi, BorderLayout.NORTH);
			panelLMonDuocGoi.setSize((int) panelLMonDuocGoi.getSize().getWidth(), 50);
			panelLMonDuocGoi.setLayout(new BorderLayout(0, 0));
			
			final JScrollPane scrollPane_3 = new JScrollPane();
			scrollPane_3.setBorder(null);
			panelLMonDuocGoi.add(scrollPane_3, BorderLayout.CENTER);
			
			panelGoiMon = new JPanel();
			panelGoiMon.setBackground(Color.WHITE);
			panelGoiMon.addContainerListener(new ContainerAdapter() {
				@Override
				public void componentAdded(ContainerEvent arg0) {
					panelStatus.updateUI();
				}
			});
			panelGoiMon.setBorder(new TitledBorder(null, "", TitledBorder.LEFT, TitledBorder.BELOW_BOTTOM, null, null));
			scrollPane_3.setViewportView(panelGoiMon);
			FlowLayout flowLayout = (FlowLayout) panelGoiMon.getLayout();
			flowLayout.setAlignment(FlowLayout.LEFT);
			flowLayout.setAlignOnBaseline(true);
			flowLayout.setVgap(0);
			
			JSplitPane splitPane = new JSplitPane();
			jContentPane.add(splitPane, BorderLayout.CENTER);
			
			tabbedPaneBan = new JTabbedPane(JTabbedPane.TOP);
			tabbedPaneBan.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent arg0) {
//			          JTabbedPane sourceTabbedPane = (JTabbedPane) arg0.getSource();
//			          MyJPanelKhuVucBan kv = (MyJPanelKhuVucBan)sourceTabbedPane.getSelectedComponent();
//			          JOptionPane.showInputDialog(kv.GetValue().getTen());
				}
			});
			
			tabbedPaneBan.setToolTipText("Các bàn trong khu vực bán");
			splitPane.setRightComponent(tabbedPaneBan);
			
			splitPane_1 = new JSplitPane();
			splitPane_1.setMinimumSize(new Dimension(300,30));
			splitPane.setLeftComponent(splitPane_1);
			
			JScrollPane scrollPane = new JScrollPane();
			splitPane_1.setLeftComponent(scrollPane);
			
			listNhomMon = new JList();
			listNhomMon.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			listNhomMon.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent arg0) {
					ChonItemNhomMon(listNhomMon, listMon);
				}
			});
			listNhomMon.setToolTipText("Danh  sách nhóm món");
			scrollPane.setViewportView(listNhomMon);
			
			
			lblDanhSachNhom = new JLabel("DANH SÁCH NHÓM MÓN");
			lblDanhSachNhom.setHorizontalAlignment(SwingConstants.CENTER);
			scrollPane.setColumnHeaderView(lblDanhSachNhom);
			
			JScrollPane scrollPane_1 = new JScrollPane();
			splitPane_1.setRightComponent(scrollPane_1);
			
			listMon = new JList();
			listMon.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					if(arg0.getButton() == MouseEvent.BUTTON1 && arg0.getClickCount() == 2)
					{	ttMon.GoiMon();
						panelStatus.updateUI();
					}else if(arg0.getButton() == MouseEvent.BUTTON3)
						ChonItemMon(listMon);										
				}
			});
			listMon.addListSelectionListener(new ListSelectionListener() {
				public void valueChanged(ListSelectionEvent arg0) {					
					ChonItemMon(listMon);
				}
			});
			listMon.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			listMon.setToolTipText("Danh sách món");
			scrollPane_1.setViewportView(listMon);
			
			lblDanhSachMon = new JLabel("DANH SÁCH MÓN");
			lblDanhSachMon.setHorizontalAlignment(SwingConstants.CENTER);
			scrollPane_1.setColumnHeaderView(lblDanhSachMon);
		}
		return jContentPane;
	}
	
	
	/*
	 **************************************************************************************
	 * các phương thức xữ lý
	 **************************************************************************************
	 */
	private boolean MonLoading = false;
	
	
	//hien thi cac thong tin tren form ban hang
	public void LoadData()
	{
		
		this.listMon.setCellRenderer(new MyJlistItemMon());
		this.listNhomMon.setCellRenderer(new MyJlistItemNhomMon());
		
		LoadDanhSachNhomMon();
		this.listNhomMon.setSelectedIndex(0);
		
		LoadDanhSachKhuVucBan();
		
		new MyControl().LoadDanhSachGoiMon(tabbedPaneBan, panelGoiMon);
		
	}
	//hien thi danh sach nhom mon
	public void LoadDanhSachNhomMon()
	{		
		NhomMonDAO DAONhomMon = new NhomMonDAO();
		@SuppressWarnings("unchecked")
		List<NhomMon> L = DAONhomMon.findAll(NhomMon.class);
		
		this.listNhomMon.setListData(L.toArray());
	}
	//xu ly su kien chon mot nhom mon
	public void ChonItemNhomMon(JList LNhomMon, JList LMon)
	{
		MonLoading = true;
		NhomMon n = (NhomMon)LNhomMon.getSelectedValue();
		LoadDanhSachMon(LMon, n.getMaSo());
		MonLoading = false;
	}
	//hien thi danh sach mon theo nhom mon duoc chon
	public void LoadDanhSachMon(JList LMon, long maNhomMon)
	{		
		MonDAO DAOMon = new MonDAO();
		List<Mon> L = (List<Mon>) DAOMon.TimTheoNhomMon(maNhomMon);
		
		LMon.setListData(L.toArray());
	}
	//thao tac xu ly khi nguoi dung chon mot mon trong danh sach mon
	@SuppressWarnings("deprecation")
	public void ChonItemMon(JList LMon)
	{
		try {
			if(LMon.getComponentCount()>0 && !MonLoading){
				Point p = MouseInfo.getPointerInfo().getLocation();
				ttMon.setLocation(Common.GetBestLocation(p, ttMon.getWidth(), ttMon.getHeight()));
				
				Mon m = (Mon)LMon.getSelectedValue();
				ttMon.SetInfo(m);
				
				ttMon.show();
			}else
				ttMon.hide();
		} catch (Exception e) {
			ttMon.hide();
		}
	}
	
	//hien thi danh sach khu vuc ban
	public void LoadDanhSachKhuVucBan( )
	{
		KhuVucBanDAO DAOkv = new KhuVucBanDAO();
		//@SuppressWarnings("unchecked")
		List<KhuVucBan> Lkv = (List<KhuVucBan>) DAOkv.findAll(KhuVucBan.class);
		
		for(KhuVucBan kv : Lkv)
		{
			MyJPanelKhuVucBan p = new MyJPanelKhuVucBan(kv, this.ttGoiMon, this.tabbedPaneBan, this.panelGoiMon);
			//p.SetImage(kv.getImageIcon().getImage());
			this.tabbedPaneBan.addTab(kv.getTen(), null , p, kv.getMoTa());
			
			try {
				new MyControl().LoadDanhSachBan(p,this.tabbedPaneBan, this.panelGoiMon);
			} catch (Exception e) {
				JOptionPane.showInputDialog( e.getMessage());
			}
		}
	}
	
	//goi mon
	//cach xu ly: 
	//		- kiem tra luong nguyen lieu dung de pha che
	//		- them mon duoc chon vao danh sach goi mon
	//		- tru luong nguyen lieu can thiet
	public void GoiMon()
	{
		ttMon.GoiMon();
	}
	//tam ngung ban
	public void TamDung()
	{
		this.setVisible(false);
	}
}
